import java.util.HashMap;

public class GameLogic implements IGameLogic {
	private State globalState;
	private final static int cutoffDepth = 20;
	private static int nextMove = 0; 

	public void initializeGame(int cols, int rows, int playerID){
		globalState = new State(cols, rows, playerID);
	}

	public int gameFinished() {
		return globalState.terminateTest();
	}

	public void insertCoin(int col, int playerID) {
		globalState.insertCoin(col, playerID);
	}

	public int decideNextMove() {
		//		System.out.println("player ID: " + s.playerID);
		//		int iPick = (int) (Math.random() * s.openCols.size());
		//		return s.openCols.get(iPick);

		maxValue(globalState, "", cutoffDepth);
		//		System.out.println("minimax result: " + result.value + "   next: " + result.action);
		//		System.out.println("-----------------------------------------------");
		//		System.out.println(s.playerID);
		return nextMove;
	}

	public static int minimaxAdapter(int winner, State s, int depth) {
		if (winner == 0) {	//   game not over
			return 0;
		} else if (winner == s.ID) {     // computer player won
			return 10000 + depth;
		} else if (winner != s.ID ) {    // computer player lost
			return -10000 + (cutoffDepth - depth);  
		} else {
			return -100 + (cutoffDepth - depth); 	// Tie
		}
	}

	public static int maxValue(State s, String space, int depth) {
		int outcome = minimaxAdapter(s.terminateTest(), s, depth);
		if (outcome != 0) {
//			System.out.println("outcome: " + outcome);
			return outcome;
		}
		if (depth == 0) {
			return 0;
		}
		int v = Integer.MIN_VALUE;

		//		System.out.println(space + "max");
		//		int[] vals = new int[actions.size()];
		//		int c = 0;
		for (Integer openCol : s.openCols) {
			State nextState = new State(s);
			nextState.insertCoin(openCol, s.playerID);
			nextState.switchTurn();
			//			actions.get(i).print(space);
			int val = minValue(nextState, space + " ", depth - 1);
			//			vals[c++] = val;
			if (val > v) {
				nextMove = openCol;
				if (val > 0) {
					return (val);
				}
				v = val;
			}
		}
		//		System.out.print(space);
		//		for (int i : vals) {
		//			System.out.print(i + ", ");
		//		}
		//		System.out.println("\n");
		return v; 
	}

	public static int minValue(State s, String space, int depth) {
		int outcome = minimaxAdapter(s.terminateTest(), s, depth);
		//		System.out.println(space + "outcome: " + outcome + "\n");
		if (outcome != 0)
			return outcome;
		int v = Integer.MAX_VALUE;
		for (Integer nextCol : s.openCols) {
			State nextState = new State(s);
			nextState.insertCoin(nextCol, s.playerID);
			nextState.switchTurn();
			//			actions.get(i).print(space);
			
			v = Math.min(v, maxValue(nextState, space + " ", depth - 1));
		}
		return v;  
	}

	public static void main(String[] args) {
		GameLogic g = new GameLogic();
		g.initializeGame(4, 3, 1);
		g.globalState.printAll();
		g.globalState.insertCoin(1, 2);
		System.out.println(GameLogic.maxValue(g.globalState, "", cutoffDepth));
	}

}
